package com.wwwarehouse.taskcenter.adapter.job_point;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wwwarehouse.common.tools.ConvertUtils;
import com.wwwarehouse.common.tools.ScreenUtils;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.bean.job_point.AddJobPointStep6ResponseBean;

import java.util.ArrayList;

import de.greenrobot.event.EventBus;

/**
 * 定义实物资源的主页面的adapter。
 *
 */

public class JobPointChooseObject6Adapter extends RecyclerView.Adapter<JobPointChooseObject6Adapter.ViewHolder> {
    private ArrayList<AddJobPointStep6ResponseBean.ListBean> lists = new ArrayList<>();
    private Context mContext;
    private View view;
    private int itemHeight;

    private void caculateItemHeight(){
        int screenHeight = ScreenUtils.getScreenHeight(mContext);
        int barHeight = ScreenUtils.getStatusHeight(mContext);

        itemHeight = (screenHeight - barHeight - ConvertUtils.dip2px(mContext,120)) / 10;

    }

        public JobPointChooseObject6Adapter(ArrayList<AddJobPointStep6ResponseBean.ListBean> lists , Context mContext){
        this.lists = lists;
        this.mContext = mContext;
        caculateItemHeight();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        view = LayoutInflater.from(mContext).inflate(R.layout.item_taskpoint_adapter_relativething, parent, false);
        ViewHolder holder = new ViewHolder(view);

        RecyclerView.LayoutParams params =  new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        params.height = itemHeight;
        view.setLayoutParams(params);


        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        final AddJobPointStep6ResponseBean.ListBean bean = lists.get(position);
        holder.itemTxt.setText("" + bean.getBusinessUnitName());


        if(bean.isClick()){
            holder.itemImg.setVisibility(View.VISIBLE);
        }else{
            holder.itemImg.setVisibility(View.GONE);
        }

        holder.itemRel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bean.setClick(!bean.isClick());
                notifyDataSetChanged();
                EventBus.getDefault().post(bean);
            }
        });

    }

    @Override
    public int getItemCount() {
        return lists.size();
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView itemTxt;
        ImageView itemImg;
        RelativeLayout itemRel;
        public ViewHolder(View itemView) {
            super(itemView);
            itemRel = (RelativeLayout)itemView.findViewById(R.id.task_point_rel);
            itemTxt = (TextView)itemView.findViewById(R.id.task_point_relativething_item_txt);
            itemImg = (ImageView)itemView.findViewById(R.id.task_point_relativething_item_img);
        }
    }
}
